<template>
    <div class="chat">
        <van-nav-bar fixed placeholder title="客服" left-arrow @click-left='fh' />
        <div id="content" ref="rightBody">
        <div style="text-align: center ; color: #787878;font-size: 13px;">
            <span v-show="show" @click="fy" class="check">点击查看更多</span>
        </div>
            <div v-for="item in list" :key="item.id">
                <div class="chat-model" style="justify-content:flex-end" v-if="item.isself">
                    <div style="position: absolute; right: 50px; top: -3px;"><span
                            style="font-size:10px; color: #a9a9a9;">{{ item.send_time }}</span></div>
                    <div class="chat-content chat-content-r">
                        {{ item.content }}
                    </div>
                    <div>
                        <van-image width="45px" height="45px" fit="fill" round :src="item.sender_avatar" />
                    </div>
                </div>
                <div class="chat-model" v-else>
                    <div>
                        <van-image width="45px" height="45px" fit="fill" round :src="item.sender_avatar" />
                    </div>
                    <div>
                        <div style="margin-bottom:-10px ;"><span
                                style="font-size: 10px;">{{ item.sender_nickname }}</span><span
                                style="font-size:10px ;margin-left: 10px;color: #a9a9a9;">{{ item.send_time }}</span>
                        </div>
                        <div class="chat-content chat-content-l">
                            {{ item.content }}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="bottom">
            <div id="left"> <input type="text" v-model="text" />
                <van-button @click="onSend">发送</van-button>
                <div id="icon" @click="isActive = !isActive">
                    <van-icon name="add-o" />
                </div>
            </div>
            <div v-show="isActive" class="box" style="display: flex;">
                <div v-for="item in box" :key="item.span"
                    style="margin: 10px;  display: flex; flex-direction: column; justify-content: space-evenly; align-items: center;">
                    <div
                        style="border-radius: 1px; background-color: #fff; height: 50px ; width: 50px; text-align: center;">
                        <van-icon class="wy" :name="item.name" />
                    </div>
                    <span style="font-size: 10px; color: darkgray;">{{ item.span }}</span>
                </div>
            </div>
        </div>

    </div>
</template>
  
<script>
export default {
    name: 'HelloWorld',
    data() {
        return {
            show: false,
            box: [{ name: 'photo', span: '图片' }, { name: 'bag', span: '商品详情' }],
            text: '',
            list: [],
            isActive: false,
            orderid: "",
            stime: "",
            limit: 2,
            page: 1,
            total: 0
        }
    },
    async created() {
        await this.getData();
        this.send();
        if (this.total>2) {
            this.show = true 
        }
        
    },
    methods: {
        async getData() {
            this.orderid = this.$route.query.orderid;
            let res = await this.$frontaxios.post(this.$front.getKeFuChat, { orderid: this.orderid, limit: this.limit, page: this.page });
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
        },
        async onSend() {
            if (!this.text) {
                this.$toast('请输入内容')
                return;
            }
            var data = {
                orderid: this.orderid,
                content: this.text,
                isself: 1,
            }
            let res = await this.$frontaxios.post(this.$front.sendKeFuChat, data);
            console.log(res);
            this.list.push(data);
            if (res.code == 1) {
                this.text = '';
                this.$refs.rightBody.scrollTop = this.$refs.rightBody.scrollHeight - this.$refs.rightBody.clientHeight;
            }
        },
        send() {
            this.stime = setInterval(() => {
                this.getData();
            }, 5000);
        },
        fh() {
            clearInterval(this.stime)
            this.$router.back()
        },
        async fy() {
            this.page ++
            let res = await this.$frontaxios.post(this.$front.getKeFuChat, { orderid: this.orderid, limit: this.limit, page: this.page });
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
            if(this.list.length<2){
                this.show=false
            }
        }
    },

    destroyed() {
        // 销毁监听
        clearInterval(this.stime);
    },
}
</script>
  
<style>
.chat {
    height: 100vh;
    background-color: #f1f1f3;
}

#content {
    overflow: auto;
    height: 100vh;
    padding-bottom: 300px;
    background-color: #f1f1f3;
}

#bottom {
    position: fixed;
    width: 100%;
    bottom: 0px;
    /* display: flex;
    justify-content: space-evenly;
    padding: 10px 0px; */
    background-color: #F1F1F3;
}

#left {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    padding: 10px 0px;
    background-color: #F1F1F3;
}

#bottom input {
    background-color: white;
    width: 72%;
    height: 30px;
    padding: 3px 5px;
    vertical-align: sub;
    border-style: none;
    border-radius: 5px;
}

#bottom button {
    height: 32px;
    background-color: rgb(245, 158, 1);
    border-radius: 5px;
    color: #fff;
}

.chat-model {
    display: flex;
    flex-direction: row;
    margin: 10px 10px;
    margin-top: 30px;
    align-items: center;
    position: relative
}

.chat-content {
    position: relative;
    max-width: 67%;
    word-break: break-all;
    word-wrap: break-word;
    top: 18px;
    padding: 10px;
    border-radius: 5px;
    background-color: white;
}

.chat-content-r {
    right: 10px;
}

.chat-content-l {
    left: 10px;
}

#icon {
    font-size: 25px;
    margin-top: 2px;
}

.box {
    width: 100%;
    height: 100px;
}

.box .van-icon {
    font-size: 40px;
    color: dimgray;
}
.check{
    margin: 0 auto;
}
</style>